import 'package:dio/dio.dart';
import 'package:flutter_changdian_football/api/api.dart';
import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/http_utils.dart';
import 'package:flutter_changdian_football/global.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class ReqUser {

  /// 获取用户个人主页基本信息
  static Future<ApiResponse<dynamic>> selfCenter() async {
    try {
      var response = await HttpUtils.get(Api.selfCenter);
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 获取用户自己的个人信息
  static Future<ApiResponse<dynamic>> selfDetail() async {
    try {
      var response = await HttpUtils.get(Api.selfDetail);
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 新增用户球员信息或更新球员信息
  static Future<ApiResponse<dynamic>> playerInfo(
    {
      String? birthday,
      String? cityCode,
      String? cityName,
      String? goodPositionCode,
      int height = 0,
      String? imageUrl,
      String? name,
      String? provinceCode,
      String? provinceName,
      int sex = 0,
      int weight = 0
    }
  ) async {
    
    try {
      var response = await HttpUtils.post(Api.playerInfo, data: {
        "birthday": birthday,
        "cityCode": cityCode,
        "cityName": cityName,
        "goodPositionCode": goodPositionCode,
        "height": height,
        "imageUrl": imageUrl,
        "name": name,
        "provinceCode": provinceCode,
        "provinceName": provinceName,
        "sex": sex,
        "weight": weight
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 余额
  static Future<ApiResponse<dynamic>> userAccountBalance() async {
    try {
      var response = await HttpUtils.get(Api.userAccountBalance);
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 个人消费记录和充值记录
  static Future<ApiResponse<dynamic>> userAccountRecord(
    {
      int pageNum = 1,
      required String recordType
    }
  ) async {
    try {
      var response = await HttpUtils.post(Api.userAccountRecord, data: {
        'pageNum': pageNum,
        'pageSize': Global.pageSize,
        'recordType': recordType
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 获取用户绑定的支付宝信息
  static Future<ApiResponse<dynamic>> getUserZfbInfo() async {
    try {
      var response = await HttpUtils.get(Api.getUserZfbInfo);
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 用户关注球员列表
  static Future<ApiResponse<dynamic>> getUserInterestList(
    {
      int pageNum = 1,
    }
  ) async {
    try {
      var response = await HttpUtils.post(Api.getUserInterestList, data: {
        'pageNum': pageNum,
        'pageSize': Global.pageSize,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 退出登录
  static Future<ApiResponse<dynamic>> logout() async {
    try {
      var response = await HttpUtils.post(Api.logout);
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 获取登陆验证码
  static Future<ApiResponse<dynamic>> sendVerificationCode({
    required String telephone,
    required String type
  }) async {
    try {
      var response = await HttpUtils.post(Api.sendVerificationCode, data: {
        "telephone": telephone,
        "type": type
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 验证码登录
  static Future<ApiResponse<dynamic>> loginByVerificationCode({
    required String telephone,
    required String captcha
  }) async {
    try {
      var response = await HttpUtils.post(Api.loginByVerificationCode, data: {
        "telephone": telephone,
        "captcha": captcha
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 用户详情查看接口
  static Future<ApiResponse<dynamic>> userDetail({
    required String queriedUserId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.userDetail, data: {
        "queriedUserId": queriedUserId,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 用户点赞或取消点赞球员
  static Future<ApiResponse<dynamic>> praiseOrCancelPraise({
    required String focusedUserId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.praiseOrCancelPraise, data: {
        "praisedUserId": focusedUserId,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 用户关注或取关球员
  static Future<ApiResponse<dynamic>> focusOrCancelFocus({
    required String focusedUserId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.focusOrCancelFocus, data: {
        "focusedUserId": focusedUserId,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 名人堂功能接口
  static Future<ApiResponse<dynamic>> userRankList({
    String? keyword,
    int pageNum = 1,
    String? pcode
  }) async {
    try {
      var response = await HttpUtils.post(Api.userRankList, data: {
        "keyword": keyword,
        "pageNum": pageNum,
        "pageSize": Global.pageSize,
        "pcode": pcode
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 用户单条消息已读
  static Future<ApiResponse<dynamic>> readMessage({
    required String messageId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.readMessage, data: {
        "messageId": messageId,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 用户全部已读
  static Future<ApiResponse<dynamic>> readAll() async {
    try {
      var response = await HttpUtils.post(Api.readAll);
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }
  

}